<template>
  <div>
    <v2-table ref="v2Table" :table-config="tableConfig" :inline="false" @add="dialogForm = true" :tree="false"
              @updateDict="updateDict"
              @addRowDict="addRowDict"></v2-table>
    <dialog-form :columns="columns" :form-data="formData" v-model="dialogForm" @submit="addDict" ref="df"></dialog-form>

    <toolDictItem v-model="dictItemDialog" :dictType="formData.dictType" :dictId="formData.dictId"></toolDictItem>
  </div>
</template>

<script>
import Service from "api/Service";
import DialogForm from "components/v2-form/dialogForm";
import http from "network/http";
import ApiResponse from "utils/ApiResponse";
import dictConfig from "./dictConfig"
import toolDictItem from "views/admin/ds/toolDictItem";

export default {
  name: "toolDict" ,
  components: {DialogForm,toolDictItem},
  data(){
    return {
      formData:{} ,
      dictItemColumns: dictConfig.dictItemColumns ,
      dictItemDialog: false ,
      tableConfig:{
        btnAlign: 'right',
        url: `/${Service.itToolService}/toolDict/listByPage`,
        btns:[
          {
            title: '新增',
            type: 'ADD',
            callback: 'add'
          }
        ],
        tableColumns:[
          {
            title: '字典名称' ,
            key: 'dictTitle',
            required: true
          },
          {
            key: 'dictType' ,
            title: "字典类型",
            required: true
          },
          {
            key: 'createTime',
            show: false ,
            title: '创建时间'
          },
          {
            key:'action',
            title: '操作',
            btns:[
              {
                title: '修改',
                disabledFiled: ['dictType'],
                callback: 'updateDict'
              },
              {
                title: '详情',
                callback: 'addRowDict'
              }
            ]
          }
        ]
      } ,
      dialogForm: false ,
      api:{
        update: `/${Service.itToolService}/toolDictItem/update`,
        save: `/${Service.itToolService}/toolDictItem/save`
      },
      apiKey: '' ,
      columns: [
        {
          title: '字典名称' ,
          key: 'dictTitle',
          required: true
        },
        {
          title: '字典类型' ,
          key: 'dictType',
          required: true
        },
        {
          title: '字典描述' ,
          key: 'desc'
        }
      ]
    }
  },
  methods:{
    addDict(formData){
      http.post(`/${Service.itToolService}/toolDict/save`,formData).then(res=>{
        ApiResponse.init(res)
            .print(res.msg)
            .ok(()=>{
              this.$refs.v2Table.refresh()
        })
      })
    },
    addRowDict({row}){
      this.formData = {} ;
      this.formData.dictType = row.dictType ;
      this.formData.dictId = row.dictId ;
      this.apiKey = "save" ;
      this.dictItemDialog = true
    },
    updateDict({row}){
      this.formData = {}
      this.formData = row;
      this.apiKey = "update" ;
      this.dictItemDialog = true
    },
    addDictItem(formData){
      let api = this.apiKey ;
      http.post(this.api[api],formData).then(res=>{
        ApiResponse.init(res)
            .print(res.msg)
            .ok(()=>{
              this.$refs.v2Table.refresh()
            })
      })
    }
  }
}
</script>

<style scoped>

</style>